<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ActivityModel;
use app\admin\model\CommonModel;
use app\admin\model\ShopOrderModel;
use cmf\controller\AdminBaseController;
use think\Db;

//商品订单管理
class ShopOrderController extends AdminBaseController
{
    public function index(){
        $param = $this->request->param();
        $status = ShopOrderModel::STATUS;
        //商品分类
        $shop_category = $this->getShopCategory();
        $list = Db::name('shop_order')
            ->where(function($query) use($param){
                if(isset($param['shop_order_sn']) && !empty($param['shop_order_sn'])){
                    $shop_order_sn = $param['shop_order_sn'];
                    $query->where('shop_order_sn','like','%'.$shop_order_sn.'%');
                }

                if(isset($param['shop_id']) && $param['shop_id'] != '-1'){
                    $query->where('shop_id',$param['shop_id']);
                }
                if(isset($param['status_id']) && $param['status_id'] != '-1'){
                    $query->where('status',$param['status_id']);
                }
            })
            ->field('*')
            ->order('id desc')
            ->paginate(null,false,['query'=>$param])
            ->each(function($item, $key) use($status,$shop_category){
                $item['status1'] = $status[$item['status']];
                $item['shop_id'] = $shop_category[$item['shop_id']];
                $item['deliver_time'] = !empty($item['deliver_time'])?date('Y-m-d H:i:s',$item['deliver_time']):'';
                $item['finish_time'] = !empty($item['finish_time'])?date('Y-m-d H:i:s',$item['finish_time']):'';
                return $item;
            });
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        //待发货
        $this->assign('delivers',ShopOrderModel::DELIVERS);
        //商品分类
        $this->assign('shop_category',$shop_category);
        //订单状态
        $this->assign('status',ShopOrderModel::STATUS);

        //搜索商品分类回显
        $this->assign('shop_id',$param['shop_id']??-1);
        //搜索订单状态回显
        $this->assign('status_id',$param['status_id']??-1);
        return $this->fetch();
    }

    //查询商品分类
    private function getShopCategory(){
        $commonModel = new CommonModel();
        $res = $commonModel->selectData('shop_category','id,shop_name');
        $res_shop = [];
        foreach($res as $value){
            $res_shop[$value['id']] = $value['shop_name'];
        }
        return $res_shop;
    }

    //发货
    public function deliver(){
        $id = $this->request->param('id');
        $orderModel = new ShopOrderModel();
        $res = $orderModel->save(['deliver_time'=>time(),'status'=>ShopOrderModel::RECEIPT],['id'=>$id]);
        if($res){
            $this->success('发货成功!', url('ShopOrder/index'));
        }else{
            $this->error('发货失败');
        }
    }
}